# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

# The patches for this package are developed inside the -git package.
# This is for three reasons:
# 1. Upstreaming is easier.
# 2. The Qt devs often forget to release Qbs at the same time as Qt Creator.
# 3. Commit messages are retained (though efforts are made for that via git init)

# TODO :: Patch this for clang support:
#         qbs setup-toolchains --type clang /mingw64/bin/clang.exe clang
#         .. will add a profile to %AppData%/QtProject/qbs.ini that contains:
#         qt-project\qbs\profiles\clang\qbs\toolchain=clang, llvm, gcc
#         .. but is missing:
#         qt-project\qbs\profiles\clang\qbs\targetOS=windows
#         then in share/qbs/modules/cpp/windows-mingw.qbs we need to change
#         condition: qbs.targetOS.contains("windows") && qbs.toolchain.contains("mingw")
#         .. to:
#         condition: qbs.targetOS.contains("windows") \
#               && (qbs.toolchain.contains("mingw") || qbs.toolchain.contains("gcc"))

_realname=qbs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.11.0
pkgrel=2
_pkgfqn="${_realname}-src-${pkgver}"
pkgdesc="Qt Build Suite (mingw-w64)"
url='https://www.qt.io/'
arch=('any')
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("git")
options=('docs' 'staticlibs') # '!strip' 'debug')
source=("https://download.qt.io/official_releases/qbs/${pkgver}/${_pkgfqn}.tar.gz"
        #${_realname}-${pkgver}::"git://gitorious.org/qt-labs/qbs.git#tag=v${pkgver}"
        "manifest"
        "0003-add-generator-header-installs.1.4.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch"
        "0005-setup-toolchains-Pretend-msys-is-mingw.1.4.patch"
        "0006-Add-some-missing-files.1.4.patch"
        "0008-add-many-files-to-installed-headers.1.4.patch"
        "0009-fix-installed-headers-list.patch")
sha256sums=('6ce30c468e688f6843468324a34a7191409b471c3ff6f3e834ef8bfa7b3467cd'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855'
            '18f1bfe2434ccf97e1366a96b83bdc41bbd4399185bb701d061710b292a5ad59'
            'a6f3316cd4756ba60404ac17f0a60aa573b99c08501f3f423f4f71052e149ebc'
            '1cd74c4dec1ecc996cdd783c33450633a100e82ab5a58169f65454668879b201'
            '40d53fca4e80ab63baa8649bae43882daf2c91de22412c71dec712ac59f4a39e'
            '664e51c79358d8fb180b6ee143fd3b1ad374979f1c1283038eda40d71d7727d2'
            '4f125ee78f66460dbe6a647728fcc562738d693cc3c30b1da13eb04010d64f2d')

prepare() {
  cd ${_pkgfqn}
  #[[ -d src/plugins/generator ]] && rm -rf src/plugins/generator
  #rm -rf src/lib/corelib/api/generator.*
  rm -rf src/lib/corelib/tools/generat*
  if [ -d .git ]; then
    git reset --hard $(git rev-list --max-parents=0 --abbrev-commit HEAD)
  else
    git init .
    git add -A
    git commit -m "Initial git import"
  fi

  git am "${srcdir}"/0003-add-generator-header-installs.1.4.patch
  git am "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch
  git am "${srcdir}"/0005-setup-toolchains-Pretend-msys-is-mingw.1.4.patch
  git am "${srcdir}"/0006-Add-some-missing-files.1.4.patch
  git am "${srcdir}"/0008-add-many-files-to-installed-headers.1.4.patch
  git am "${srcdir}"/0009-fix-installed-headers-list.patch
}

build() {
  local _config
  if check_option "debug" "n"; then
    _config="release"
  else
    _config="debug"
  fi
  cd ${srcdir}
  
  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH}
  cd build-${CARCH}
  
  export MSYS2_ARG_CONV_EXCL="QBS_INSTALL_PREFIX="
  ${MINGW_PREFIX}/bin/qmake.exe \
    QBS_INSTALL_PREFIX="${MINGW_PREFIX}" \
    CONFIG+=${_config} \
    CONFIG+=qbs_enable_project_file_updates \
    ../${_pkgfqn}/qbs.pro
  
  # Because xcodeprojectgenerator has a build order problem.
  make
  make docs
}

package() {
  cd ${srcdir}/build-${CARCH}
  
  export MSYS2_ARG_CONV_EXCL="QBS_INSTALL_PREFIX="
  make INSTALL_ROOT=${pkgdir} install
  make INSTALL_ROOT=${pkgdir} install_docs

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll

  for ff in qbs-setup-toolchains qbs-setup-qt; do
    cp ${srcdir}/manifest ${pkgdir}${MINGW_PREFIX}/bin/${ff}.exe.manifest
  done
  cp ${srcdir}/${_pkgfqn}/VERSION ${pkgdir}${MINGW_PREFIX}/include/qbs
}
